package com.lh.lock;

import java.util.concurrent.TimeUnit;


public class MyVolatile {

    static  boolean flag = true;

    static volatile int nums = 0;

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            while (flag) {
                System.out.println("进入循环!!!");
            }
            System.out.println("退出线程!!!");
        }, "A").start();

        TimeUnit.SECONDS.sleep(2);

        new Thread(() -> {
            flag = false;
            System.out.println("进入线程B!!!");
        }, "B").start();

        System.out.println("--------------------");

        //add();
    }

    public static void add() throws InterruptedException {

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    nums++;   //++操作不具备原子操作
                }
            }).start();
        }
        TimeUnit.SECONDS.sleep(2);

        System.out.println(nums);

    }
}
